package middle.id_1_100;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved.
 * Description: 上机编程认证
 * Note: 缺省代码仅供参考，可自行决定使用、修改或删除
 */

import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Scanner;

/**
 * OJ考题代码：月活数与日活数统计
 *
 * @author 命题组
 * @since 2020-1-22
 */
public class hua19_liang {
    // 待实现函数，在此函数中填入答题代码
    private static int[] getActiveUserNum(String[] logs) {
        int daysOfMonth = 31;
        Map<Integer, Set<String>> dailyActivate = new HashMap<>();
        Set<String> monthIps = new HashSet<>();
        for (String log : logs) {
            String[] splits = log.split("\\|");
            String date = splits[0];
            int day = Integer.valueOf(date.split("\\-")[2]);
            String ip = cleanIp(splits[1]);
            String address = splits[2];
            String status = splits[3];
            if (!address.equals("/login.do") || !status.equals("success")) {
                // 如果登陆地址不等于"./login.do" 或者状态不为"success"，则跳过
                continue;
            }
            monthIps.add(ip);
            if (!dailyActivate.containsKey(day)) {
                dailyActivate.put(day, new HashSet<>());
            }
            dailyActivate.get(day).add(ip);
        }
        int[] result = new int[daysOfMonth + 1];
        result[0] = monthIps.size();
        for (int index = 1; index <= daysOfMonth; index++) {
            result[index] = dailyActivate.getOrDefault(index, new HashSet<>()).size();
        }
        return result;
    }

    private static String cleanIp(String ip) {
        String[] splits = ip.split("\\.");
        return String.valueOf(Integer.parseInt(splits[0])) + "."
                + String.valueOf(Integer.parseInt(splits[1])) + "."
                + String.valueOf(Integer.parseInt(splits[2])) + "."
                + String.valueOf(Integer.parseInt(splits[3]));
    }
}

